package com.neolix.watch;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neolix.watch.bluetooth.BlueToothUtil;
import com.neolix.watch.util.Constant;
import com.neolix.watch.util.SharedPreferencesUtil;
import com.neolix.watch.util.StringUtil;
import com.neolix.watch.view.SlipButton;

import static com.neolix.watch.NeolixApplication.getInstance;
import static com.neolix.watch.util.SharedPreferencesUtil.getGoalSteps;
import static com.neolix.watch.util.SharedPreferencesUtil.getLoseDistance;
import static com.neolix.watch.util.SharedPreferencesUtil.isSendInComingInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.isSendSMSInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.isSendWXInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.isSleepMode;
import static com.neolix.watch.util.SharedPreferencesUtil.setIsSendInComingInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.setIsSendSMSInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.setIsSendWXInfo;
import static com.neolix.watch.util.SharedPreferencesUtil.setSleepMode;
import static com.neolix.watch.util.StringUtil.formatTemplateString;
import static com.neolix.watch.util.StringUtil.showText;

public class SettingActivity extends NeolixNaviagationBaseActivity {

    private SlipButton inComingSlipButton;
    private SlipButton messageSlipButton;
    private SlipButton wxSliptButton;
    private SlipButton sleepSlipButton;
    private View lostDistanceView;
    private View goalSetpsView;
    private TextView loseDistanceTextView;
    private TextView goalStepsTextView;
    private TextView deviceNameTextView;
    private TextView bluetoothBindTextView;
    private TextView sleepTimeTextView;
    private View bluetoothView;

    public SettingActivity() {
        super(R.layout.activity_setting);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.setting);

        initUi();
        listener();
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
            BlueToothUtil.connectDevice(SettingActivity.this);
        } else if (requestCode == Constant.REQUEST_SCAN_BLUETOOTH && resultCode == Activity.RESULT_OK) {
            initBluetoothInfo();
        } else if (requestCode == Constant.REQUEST_CONNECT_DEVICE && resultCode == Activity.RESULT_OK) {
            initBluetoothInfo();
        }
    }

    private void initData() {
        inComingSlipButton.setCheck(isSendInComingInfo());
        messageSlipButton.setCheck(isSendSMSInfo());
        wxSliptButton.setCheck(isSendWXInfo());
        boolean isSleepMode = isSleepMode();
        sleepSlipButton.setCheck(isSleepMode);

        loseDistanceTextView.setText(formatTemplateString(R.string.format_distance, getLoseDistance()));
        goalStepsTextView.setText(String.valueOf(getGoalSteps()));

        initBluetoothInfo();

        if (isSleepMode) {
            //TODO:获取时间
        } else {
            sleepTimeTextView.setText("");
        }
    }

    private void initBluetoothInfo() {
        if (getInstance().isConnectWatch()) {
            bluetoothBindTextView.setText(getString(R.string.have_bind_device));
            deviceNameTextView.setText(getInstance().bluetoothLeService.getDeviceName());
        } else {
            bluetoothBindTextView.setText(getString(R.string.no_bind_device));
            deviceNameTextView.setText("");
        }
    }

    private void initUi() {
        inComingSlipButton = (SlipButton) findViewById(R.id.in_coming_slip_button);
        messageSlipButton = (SlipButton) findViewById(R.id.message_slip_button);
        wxSliptButton = (SlipButton) findViewById(R.id.wx_slip_button);
        sleepSlipButton = (SlipButton) findViewById(R.id.sleep_slip_button);

        lostDistanceView = findViewById(R.id.lose_distance_view);
        goalSetpsView = findViewById(R.id.goal_steps_view);
        bluetoothView = findViewById(R.id.bluetooth_view);

        loseDistanceTextView = (TextView) findViewById(R.id.lose_distance);
        goalStepsTextView = (TextView) findViewById(R.id.goal_steps);
        deviceNameTextView = (TextView) findViewById(R.id.device_name);
        bluetoothBindTextView = (TextView) findViewById(R.id.bluetooth_bind_info);
        sleepTimeTextView = (TextView) findViewById(R.id.sleep_time);
    }

    private void listener() {
        inComingSlipButton.setOnChangedListener(new SlipButton.onChangedListener() {
            @Override
            public void nnChanged(boolean checkState) {
                setIsSendInComingInfo(checkState);
            }
        });


        messageSlipButton.setOnChangedListener(new SlipButton.onChangedListener() {
            @Override
            public void nnChanged(boolean checkState) {
                setIsSendSMSInfo(checkState);
            }
        });

        wxSliptButton.setOnChangedListener(new SlipButton.onChangedListener() {
            @Override
            public void nnChanged(boolean checkState) {
                setIsSendWXInfo(checkState);
                if (checkState) {
                    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                    startActivity(intent);
                }
            }
        });

        sleepSlipButton.setOnChangedListener(new SlipButton.onChangedListener() {
            @Override
            public void nnChanged(boolean checkState) {
                setSleepMode(checkState);
                if (checkState) {
                    //TODO:设置时间区域
                } else
                    sleepTimeTextView.setText("");
            }
        });

        loseDistanceTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 设置 防丢距离
            }
        });

        goalSetpsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setGoalSteps();
            }
        });

        lostDistanceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setLoseDistance();
            }
        });

        bluetoothView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!getInstance().isConnectWatch()) {
                    BlueToothUtil.connectBluetooth(SettingActivity.this);
                }
            }
        });
    }

    private void setGoalSteps() {
        //TODO 设置运动距离
        //TODO 判断蓝牙是否连接
        if (!NeolixApplication.getInstance().isConnectWatch()) {
            showText(getString(R.string.no_connect_watch));
            return;
        }

        final EditText data;
        final LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog, null);
        data = (EditText) layout.findViewById(R.id.data_edit_text);
        AlertDialog dialog = new AlertDialog.Builder(this).setView(layout)
                .setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String value = data.getText().toString();
                        if (StringUtil.isNotBlank(value)) {
                            SharedPreferencesUtil.setGoalSteps(Integer.valueOf(value));
                            goalStepsTextView.setText(value);
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                data.setText(goalStepsTextView.getText());
            }
        });

        dialog.show();
    }

    private void setLoseDistance() {
        //TODO 设置运动距离
        //TODO 判断蓝牙是否连接
        if (!NeolixApplication.getInstance().isConnectWatch()) {
            showText(getString(R.string.no_connect_watch));
            return;
        }

        final EditText data;
        final LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog, null);
        data = (EditText) layout.findViewById(R.id.data_edit_text);
        AlertDialog dialog = new AlertDialog.Builder(this).setView(layout)
                .setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String value = data.getText().toString();
                        if (StringUtil.isNotBlank(value)) {
                            SharedPreferencesUtil.setLoseDistance(Integer.valueOf(value));
                            loseDistanceTextView.setText(formatTemplateString(R.string.format_distance, Integer.valueOf(value)));
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                String distacne = loseDistanceTextView.getText().toString();
                data.setText(distacne.substring(0, distacne.length() - 1));
            }
        });

        dialog.show();
    }
}
